import gedit, gtk, gconf

class TweaksPlugin(gedit.Plugin):
	def __init__(self):
		gedit.Plugin.__init__(self)
		self.instances = {}
		self.client = gconf.client_get_default()

	def activate(self, window):
		self.window = window
		self.instances[window] = Tweaks(window)

	def deactivate(self, window):
		self.instances[window].deactivate()

	def update_ui(self, window):
		self.instances[window].update_ui()

	def is_configurable(self):
		return True

	def update(self):
		self.instances[self.window].deactivate()
		self.instances[self.window].runTilDone(None, None)

	def dropboxChange(self, dropbox):
		self.client.set_int("/apps/gedit-2/plugins/tabTweaks/tabPosition", dropbox.get_active())
		self.update()

	def showTabsClicked(self, checkbox):
		self.client.set_bool("/apps/gedit-2/plugins/tabTweaks/hideTabs", checkbox.get_active())
		self.update()

	def paneRightClicked(self, checkbox):
		self.client.set_bool("/apps/gedit-2/plugins/tabTweaks/paneRight", checkbox.get_active())
		self.update()

	def cancel(self, param):
		self.dialog.hide()

	def create_configure_dialog(self):
		self.dialog = gtk.Dialog("Tab Setup")
		self.content = self.dialog.get_content_area()

		label = gtk.Label("Tab Position:")
		label.show()
		label.set_justify(gtk.JUSTIFY_LEFT)
		label.set_width_chars(10)

		self.dropbox = gtk.combo_box_new_text()
		self.dropbox.append_text("Right")
		self.dropbox.append_text("Left")
		self.dropbox.append_text("Top")
		self.dropbox.append_text("Bottom")
		self.dropbox.show()
		self.dropbox.set_active(self.client.get_int("/apps/gedit-2/plugins/tabTweaks/tabPosition"))
		self.dropbox.connect("changed", self.dropboxChange)

		self.showTabs = gtk.CheckButton("Hide Tabs?")
		self.showTabs.show()
		self.showTabs.set_active(self.client.get_bool("/apps/gedit-2/plugins/tabTweaks/hideTabs"))
		self.showTabs.connect("clicked", self.showTabsClicked)

		self.paneRight = gtk.CheckButton("Side Pane on the Right?")
		self.paneRight.show()
		self.paneRight.set_active(self.client.get_bool("/apps/gedit-2/plugins/tabTweaks/paneRight"))
		self.paneRight.connect("clicked", self.paneRightClicked)

		self.box = gtk.HBox()
		self.box.show()
		self.box.pack_start(label)
		self.box.pack_start(self.dropbox)

		self.vbox = gtk.VBox()
		self.vbox.pack_start(self.box)
		self.vbox.pack_start(self.showTabs)
		self.vbox.pack_start(self.paneRight)
		self.vbox.show()
		self.content.pack_start(self.vbox)

		actions = self.dialog.get_action_area()
		cancel = gtk.Button("Close")
		cancel.connect("clicked", self.cancel)
		cancel.show()
		actions.pack_end(cancel)

		return self.dialog

class Tweaks():
	def deactivate(self):
		tab = self.window.get_active_tab()
		if(tab != None):
			tab.get_parent().set_tab_pos(gtk.POS_TOP)
			#self.showTab()
			if(self.hideTabs_id != None):
				tab.get_parent().disconnect(self.hideTabs_id)
			if(self.middle_click_id != None):
				self.fileTree.disconnect(self.middle_click_id)
			if(self.bottom_panel_click_id != None):
				self.bottom_panel.disconnect(self.bottom_panel_click_id)
			tab.get_parent().set_show_tabs(True)
			if(self.movedRight):
				self.swapPanes(200)

	def update_ui(self):
		pass

	def swapPanes(self, position=None):
		paned = self.window.get_side_panel().get_parent()
		one = paned.get_child1()
		two = paned.get_child2()
		paned.remove(one)
		paned.remove(two)
		paned.pack1(two, True)
		paned.pack2(one, False)
		paned = self.window.get_side_panel().get_parent()
		(width, height) = self.window.get_default_size()
		paned.compute_position(width, 100, 100)
		if(position == None):
			self.movedRight = True
			paned.set_position(width - 200)
		else:
			self.movedRight = False
			paned.set_position(position)

	def removeUglies(self):
		self.window.get_side_panel().get_children()[0].hide()
		self.bottom_panel = self.window.get_bottom_panel()
		self.bottom_panel.get_children()[0].get_children()[1].hide()
		self.bottom_panel_click_id = self.bottom_panel.connect("button-press-event", self.closeBottomPanel)

		menu = self.window.get_children()[0].get_children()[0]
		bar = self.window.get_children()[0].get_children()[1]
		menuButton = gtk.MenuToolButton(gtk.STOCK_ADD)
		dropMenu = gtk.Menu()

		for entry in menu.get_children():
			menu.remove(entry)
			dropMenu.append(entry)
		menuButton.set_menu(dropMenu)
		menuButton.show()
		bar.insert(menuButton, 0)
		menuButton.get_children()[0].get_children()[0].hide()

	def addMiddleClick(self):
		self.fileTree = self.window.get_side_panel().get_children()[1].get_children()[0].get_children()[0].get_children()[0]
		self.middle_click_id = self.fileTree.connect("button-press-event", self.middleClick)

	def middleClick(self, treeview, event):
		if(event.button == 2):
			self.window.close_tab(self.window.get_active_tab())

	def closeBottomPanel(self, widget, event):
		if(event.button == 2):
			self.bottom_panel.hide()

	def moveTabs(self):
		position = self.client.get_int("/apps/gedit-2/plugins/tabTweaks/tabPosition")
		if(position == 0):
			self.position = gtk.POS_RIGHT
		elif(position == 1):
			self.position = gtk.POS_LEFT
		elif(position == 2):
			self.position = gtk.POS_TOP
		else:
			self.position = gtk.POS_BOTTOM
		self.window.get_active_tab().get_parent().set_tab_pos(self.position)

	def hideTabs(self, event, data):
		self.window.get_active_tab().get_parent().set_show_tabs(False)

	def runTilDone(self, event, data):
		tab = self.window.get_active_tab()
		if(tab != None):
			self.moveTabs()
			if(self.client.get_bool("/apps/gedit-2/plugins/tabTweaks/paneRight")): #MOVE SIDE PANE TO THE RIGHT
				self.swapPanes()
			if(self.hideTabs_id != None):
				tab.get_parent().disconnect(self.hideTabs_id)
			if(self.client.get_bool("/apps/gedit-2/plugins/tabTweaks/hideTabs")):
				self.hideTabs_id = tab.get_parent().connect("event", self.hideTabs)
			if(self.event_id != None):
				self.window.disconnect(self.event_id)
			self.addMiddleClick()
			self.removeUglies()

	def __init__(self, window, ready=False):
		self.movedRight = False
		self.hideTabs_id = None
		self.middle_click_id = None
		self.bottom_panel_click_id = None
		self.fileTree = None
		self.client = gconf.client_get_default()
		self.window = window
		self.show = False
		self.event_id = self.window.connect("event", self.runTilDone)
